package com.ruoyi.system.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.domain.ProductMoney;
import com.ruoyi.system.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ProductInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品明细Controller
 * 
 * @author ruoyi
 * @date 2024-07-10
 */
@RestController
@RequestMapping("/product/info")
@Slf4j
public class ProductInfoController extends BaseController
{
    @Autowired
    private IProductInfoService productInfoService;
    @Autowired
    private IProductMoneyService productMoneyService;

    /**
     * 查询产品明细列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ProductInfo productInfo)
    {
        startPage();
        List<ProductInfo> list = productInfoService.selectProductInfoList(productInfo);
        for (int i = 0; i<list.size();i++) {
            ProductMoney productMoney = new ProductMoney();
            productMoney.setProductCode(list.get(i).getProductCode());
            productMoney.setStatus("1");
            List<ProductMoney> productMonies = productMoneyService.selectProductMoneyList(productMoney);
            if(productMonies!=null&&productMonies.size()==1){
             list.get(i).setProductMoney(productMonies.get(0));
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出产品明细列表
     */
    @Log(title = "产品明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductInfo productInfo)
    {
        List<ProductInfo> list = productInfoService.selectProductInfoList(productInfo);
        for (int i = 0; i<list.size();i++) {
            ProductMoney productMoney = new ProductMoney();
            productMoney.setProductCode(list.get(i).getProductCode());
            productMoney.setStatus("1");
            List<ProductMoney> productMonies = productMoneyService.selectProductMoneyList(productMoney);
            if(productMonies!=null&&productMonies.size()==1){
                list.get(i).setProductMoney(productMonies.get(0));
            }
        }
        ExcelUtil<ProductInfo> util = new ExcelUtil<ProductInfo>(ProductInfo.class);
        util.exportExcel(response, list, "产品明细数据");
    }

    /**
     * 获取产品明细详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productInfoService.selectProductInfoById(id));
    }

    /**
     * 新增产品明细
     */
    @Log(title = "产品明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductInfo productInfo)
    {
        return toAjax(productInfoService.insertProductInfo(productInfo));
    }

    /**
     * 修改产品明细
     */
    @Log(title = "产品明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductInfo productInfo)
    {
        return toAjax(productInfoService.updateProductInfo(productInfo));
    }

    /**
     * 删除产品明细
     */
    @Log(title = "产品明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productInfoService.deleteProductInfoByIds(ids));
    }


    /**
     * 汇总三张表数据到产品表中
     * @return
     */
    @Anonymous
    @RequestMapping("/addProductInfo")
    public AjaxResult addProductInfo()
    {
        List<Map<String, Object>> list = productInfoService.selectAllInvoiceRecords();
        for (Map<String,Object> map:list) {
           String productCode = getStringValue(map,"product_code");
           String startInMoney = getStringValue(map,"plarn_price");
           String startOutMoney = getStringValue(map,"product_price");
            Map<String,Object> moneyMap = productInfoService.selectProductMoney(productCode, startInMoney,startOutMoney);
           if(moneyMap!=null&&moneyMap.size()>0){
               String plan_price = getStringValue(moneyMap, "start_in_money");
               String product_price = getStringValue(moneyMap, "start_out_money");
               if(startInMoney.equals(plan_price)&&startOutMoney.equals(product_price)){
                   //金额与之前初始价格相等 不做处理
               }else{
                   //修改最后一条记录
                   ProductMoney productMoney = new ProductMoney();
                   productMoney.setProductCode(productCode);
                   productMoney.setEndInMoney(startInMoney);
                   productMoney.setEndOutMoney(startOutMoney);
                   productMoney.setEndTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
                   productMoney.setStatus("0");
                   productMoney.setId(Long.parseLong(moneyMap.get("id").toString()));
                   productMoneyService.updateProductMoney(productMoney);

                   //添加一条新纪录
                   ProductMoney newMoney = new ProductMoney();
                   newMoney.setProductCode(getStringValue(map, "product_code"));
                   newMoney.setStartInMoney(getStringValue(map, "plarn_price"));
                   newMoney.setStartOutMoney(getStringValue(map, "product_price"));
                   newMoney.setStartTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
                   newMoney.setStatus("1");
                   productMoneyService.insertProductMoney(newMoney);
               }
           }else{

               ProductMoney productMoney = new ProductMoney();
               productMoney.setProductCode(getStringValue(map, "product_code"));
               productMoney.setStartInMoney(getStringValue(map, "plarn_price"));
               productMoney.setStartOutMoney(getStringValue(map, "product_price"));
               productMoney.setStartTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
               productMoney.setStatus("1");
               /*List<ProductMoney> moneylist = productMoneyService.selectProductMoneyList(productMoney);
               if(moneylist!=null)
               {
                   productMoneyService.updateProductMoney(productMoney);
               }
               else
               {
                   productMoneyService.insertProductMoney(productMoney);
               }*/
               productMoneyService.insertProductMoney(productMoney);

               //之前没有在Money表有记录的数据代表没有录入过
               ProductInfo productInfo = new ProductInfo();
               productInfo.setProductCode(getStringValue(map, "product_code"));
               productInfo.setProductGroup(getStringValue(map, "product_group"));
               productInfo.setNineGroup(getStringValue(map, "nine_group"));
               productInfo.setProductName(getStringValue(map, "product_name"));
               productInfo.setModel(getStringValue(map, "model"));
               productInfo.setSpecs(getStringValue(map, "specs"));
               productInfo.setBrand(getStringValue(map, "product_brand"));
               productInfo.setProvide(getStringValue(map, "provide"));
               productInfo.setPreGroup(getStringValue(map, "pre_group"));
               /*List<ProductInfo> infolist = productInfoService.selectProductInfoList(productInfo);
               if(infolist!=null)
               {
                   productInfoService.updateProductInfo(productInfo);
               }
               else
               {
                   productInfoService.insertProductInfo(productInfo);
               }*/
               productInfoService.insertProductInfo(productInfo);
           }
        }
        return AjaxResult.success("添加成功");
    }




    private static String getStringValue(Map<String, Object> map, String key) {
        Object value = map.get(key);
        if (value instanceof String) {
            return (String) value;
        }
        return null;
    }
}
